package com.gyms;

import android.content.Context;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.gyms.bean.ThreeWheelBean;
import com.gyms.view.wheel.WheelDialog;
import com.gyms.view.wheel.view.LoopView;
import com.gyms.view.wheel.view.OnItemSelectedListener;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;

import utils.DateUtil;

/**
 * Created by ltx on 17/3/30.
 * 实现下啦选择框
 */

public class TimePopuSlect extends PopupWindow implements View.OnClickListener {
    private Context mContext;
    private LayoutInflater mInflater;
    private View mContentView;


    private LoopView sportDateLoop;
    private LoopView startTime;
    private LoopView endTime;

    private ArrayList<String> dateStrList = new ArrayList<String>();
    private ArrayList<String> yearStrList = new ArrayList<String>();
    private ArrayList<String> startTimeStrList = new ArrayList<String>();
    private ArrayList<String> endTimeStrList = new ArrayList<String>();
    private String wheelStartTime = "全部"; //选中的开始时间
    private String wheelEndTime = "全部";//选中的结束时间

    public static String maxEndTime = "23:59";
    public static String minEndTime = "00:00";
    public static final String ALL_DAY="全天";
    private String currentYear = null;


    private HashMap<String, String> dateMap = new HashMap<>();
    private Handler handler = new Handler();


    private boolean issetdata = false;
    private ArrayList<String>[] values = null;
    private String selectedValue = null;
    protected ThreeWheelBean threeWheelBean = new ThreeWheelBean();
    private TextView mTvCommit;
    private TextView mTvCancel;
    private WheelDialog.OnClickSubmitListener onClickSubmitListener;


    public TimePopuSlect(Context context){
        super(context);
        this.mContext=context;
        //打气筒
        mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        //打气
        mContentView = mInflater.inflate(R.layout.select_date_wheel,null);
        //设置View
        setContentView(mContentView);
        //设置宽与高
        setWidth(WindowManager.LayoutParams.MATCH_PARENT);

        setHeight(WindowManager.LayoutParams.WRAP_CONTENT);
        setBackgroundDrawable(null);

        /**
         * 初始化View与监听器
         */
        initView();

        initListener();



    }

    /**
     * 初始化监听
     */

    private void initListener() {
        sportDateLoop.setListener(new OnItemSelectedListener() {
            @Override
            public void onItemSelected(int index) {
                currentYear = yearStrList.get(index);
                threeWheelBean.setDateStr(dateStrList.get(index));
                startTimeStrList.clear();
                endTimeStrList.clear();
                if (index == 0) {
                    setStartTime(DateUtil.getHours());
                    setEndTime(DateUtil.getHours() + 1, true);
                } else {
                    setStartTime(6);
                    setEndTime(7, true);
                }
                startTime.setItems(startTimeStrList, "33:00");
            }
        });

        startTime.setListener(new OnItemSelectedListener() {
            @Override
            public void onItemSelected(int index) {
                wheelStartTime = startTimeStrList.get(index);
                endTimeStrList.clear();
                int hour = 0;
                boolean isHaveAllStr = false;
                if (index == 0) {
                    threeWheelBean.setStartTimeStr(minEndTime);
                    threeWheelBean.setEndTimeStr(maxEndTime);
                    isHaveAllStr = true;
                } else {
                    threeWheelBean.setStartTimeStr(startTimeStrList.get(index));
                    isHaveAllStr = false;
                }
                hour = Integer.parseInt(threeWheelBean.getStartTimeStr().substring(0, 2));
                setEndTime(hour + 1, isHaveAllStr);
                if (index != 0) {
                    threeWheelBean.setEndTimeStr(endTimeStrList.get(0));
                }
                endTime.setItems(endTimeStrList, "33:00");


//                handler.post(new Runnable() {
//                    @Override
//                    public void run() {
//                        endTime.invalidate();
//                    }
//                });


            }
        });

        endTime.setListener(new OnItemSelectedListener() {
            @Override
            public void onItemSelected(int index) {
                threeWheelBean.setEndTimeStr(endTimeStrList.get(index));
            }
        });

        mTvCancel.setOnClickListener(this);
        mTvCommit.setOnClickListener(this);

    }

    /**
     * 初始化View
     */

    private void initView() {
        sportDateLoop = (LoopView) mContentView.findViewById(R.id.sport_date);
        startTime = (LoopView) mContentView.findViewById(R.id.start_time);
        endTime = (LoopView) mContentView.findViewById(R.id.end_time);
        LinearLayout mLlHomeSub= (LinearLayout) mContentView.findViewById(R.id.ll_home_sub_layout);
        mLlHomeSub.setVisibility(View.GONE);
        LinearLayout mLlFootSub= (LinearLayout) mContentView.findViewById(R.id.ll_foot_sub_layout);
        mLlFootSub.setVisibility(View.VISIBLE);

         mTvCommit= (TextView) mContentView.findViewById(R.id.select_submit_foot);
         mTvCancel= (TextView) mContentView.findViewById(R.id.select_cancel_foot);


        if (!issetdata) {
            initData();
        }

    }

    private void initData() {
        sportDateLoop.setNotLoop();
        sportDateLoop.setInitPosition(0);
        sportDateLoop.setTextSize(18);
        sportDateLoop.setItems(dateStrList, null);

        startTime.setNotLoop();
        startTime.setInitPosition(0);
        startTime.setTextSize(18);
        startTime.setItems(startTimeStrList, "33:00");

        endTime.setNotLoop();
        endTime.setInitPosition(0);
        endTime.setTextSize(18);
        endTime.setItems(endTimeStrList, "33:00");
        initListener();
    }
    public void setStartTime(int hourStart) {
        startTimeStrList.add("全部");
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.HOUR_OF_DAY, hourStart);
        calendar.set(Calendar.MINUTE, 0);
        SimpleDateFormat sdf = new SimpleDateFormat("HH:00");
        for (int i = 0; i < 22 - hourStart; i++) {
            String dateStr = sdf.format(calendar.getTime());
            startTimeStrList.add(dateStr);
            calendar.add(Calendar.HOUR_OF_DAY, 1);
        }
    }

    public void setEndTime(int hourStart, boolean isHaveAllStr) {
        if (isHaveAllStr) {
            endTimeStrList.add("全部");
        } else {
            Calendar calendar = Calendar.getInstance();
            calendar.set(Calendar.HOUR_OF_DAY, hourStart);
            calendar.set(Calendar.MINUTE, 0);
            SimpleDateFormat sdf = new SimpleDateFormat("HH:00");
            for (int i = 0; i < 23 - hourStart; i++) {
                String dateStr = sdf.format(calendar.getTime());
                endTimeStrList.add(dateStr);
                calendar.add(Calendar.HOUR_OF_DAY, 1);
            }
        }
    }

    //时间显示转换
    protected void timeStr2Integer() {
        String dateStr = dateMap.get(threeWheelBean.getDateStr());
        SimpleDateFormat sFormat = new SimpleDateFormat("yyyyE(MM月dd日)HH:mm");
        try {
            if ("全部".equals(wheelStartTime)) {
//                threeWheelBean.setStartTimeStr(startTimeStrList.get(1));
                threeWheelBean.setStartTimeStr(minEndTime);
                threeWheelBean.setEndTimeStr(maxEndTime);
                threeWheelBean.setAllDay(true);
            } else {
                threeWheelBean.setAllDay(false);
            }
            Date startDate = sFormat.parse(currentYear + dateStr + threeWheelBean.getStartTimeStr());
            Date endDate = sFormat.parse(currentYear + dateStr + threeWheelBean.getEndTimeStr());
            int timeStart = (int) (startDate.getTime() / 1000);
            int timeEnd = (int) (endDate.getTime() / 1000);
            threeWheelBean.setStartTimeInt(timeStart);
            threeWheelBean.setEndTimeLInt(timeEnd);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    //初始化设置时间
    public void setDate() {

        Calendar calendar = Calendar.getInstance();
        SimpleDateFormat sdf = new SimpleDateFormat("E(MM月dd日)");
        for (int i = 0; i < 7; i++) {
            yearStrList.add(calendar.get(Calendar.YEAR) + "");
            String dateStr = sdf.format(calendar.getTime());
            switch (i) {
                case 0:
                    dateMap.put("今天" + dateStr.substring(2), dateStr);
                    dateStr = "今天" + dateStr.substring(2);
                    break;
                case 1:
                    dateMap.put("明天" + dateStr.substring(2), dateStr);
                    dateStr = "明天" + dateStr.substring(2);
                    break;
                default:
                    dateMap.put(dateStr, dateStr);
                    break;

            }
            dateStrList.add(dateStr);
            calendar.add(Calendar.DAY_OF_MONTH, 1);

        }
        setStartTime(DateUtil.getHours());
        setEndTime(DateUtil.getHours() + 1, true);
        currentYear = yearStrList.get(0);
        threeWheelBean.setDateStr(dateStrList.get(0));
        threeWheelBean.setStartTimeStr(startTimeStrList.get(0));
        threeWheelBean.setEndTimeStr(endTimeStrList.get(0));
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.select_submit_foot:
                dismiss();
                if(null!=onClickSubmitListener){
                    timeStr2Integer();
                    onClickSubmitListener.onClickSubmit(null, threeWheelBean);
                }
                break;

            case R.id.select_cancel_foot:
                dismiss();
                if(null!=onClickSubmitListener){
                    onClickSubmitListener.onNotTimeSelect();
                }
                break;
        }
    }
    public void setOnClickSubmitListener(WheelDialog.OnClickSubmitListener onClickSubmitListener) {
        this.onClickSubmitListener = onClickSubmitListener;
    }
}
